package com.inossem.allotin.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.allotin.utils.Utils;
import com.inossem.wms.vo.cus.CusRFIDTaskReqVo;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/5/23 0023.
 * 列表页适配器
 */

public class EmergencyDetailAdapter extends RecyclerView.Adapter<EmergencyDetailAdapter.ViewHolder> {

    private Activity context;
    private List<CusRFIDTaskReqVo> list = new ArrayList<>();
    private OnItemClickListener listener;
    private boolean isEdit;

    public EmergencyDetailAdapter(Activity context, boolean isEdit) {
        this.context = context;
        this.isEdit = isEdit;
    }

    public void addList(List<CusRFIDTaskReqVo> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public void clearList() {
        this.list.clear();
        notifyDataSetChanged();
    }

    public void setListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public void removeItem(int i) {
        list.remove(i);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_emergency_detail, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i % 2 == 0) {
            viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_white));
        } else {
            viewHolder.mLayout.setBackgroundColor(ContextCompat.getColor(context, R.color.base_list_bg));
        }
        CusRFIDTaskReqVo bean = list.get(i);
        viewHolder.mTvRfid.setText(bean.getEpcCode());
        viewHolder.mTvMatCode.setText(bean.getMatCode());
        viewHolder.mTvMatName.setText(bean.getMatName());
        viewHolder.mTvQty.setTag(i);
        viewHolder.mTvQty.setText(bean.getQty().toPlainString());
        viewHolder.mTvBatch.setText(bean.getBatch());
        viewHolder.mTvQty.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                int position = (int) viewHolder.mTvQty.getTag();
                try {
                    if (TextUtils.isEmpty(s)) {
                        list.get(position).setQty(new BigDecimal(0));
                    } else {
                        list.get(position).setQty(new BigDecimal(s.toString()));
                    }
                } catch (Exception e) {
                    viewHolder.mTvQty.setText("");
                }
            }
        });
        viewHolder.mTvRemark.setOnClickListener(view -> {
            if (listener != null)
                listener.onDetailClick(i);
        });

        if (isEdit) {
        } else {
            viewHolder.mTvQty.setFocusable(false);
            viewHolder.mTvQty.setCursorVisible(false);
            viewHolder.mTvQty.setFocusableInTouchMode(false);
            viewHolder.mTvQty.setBackground(null);
            viewHolder.mTvQty.setTextColor(ContextCompat.getColor(context, R.color.text_gray));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public interface OnItemClickListener {
        void onDetailClick(int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_rfid)
        TextView mTvRfid;
        @BindView(R.id.tv_mat_code)
        TextView mTvMatCode;
        @BindView(R.id.tv_mat_name)
        TextView mTvMatName;
        @BindView(R.id.tv_qty)
        TextView mTvQty;
        @BindView(R.id.tv_batch)
        TextView mTvBatch;
        @BindView(R.id.tv_remark)
        TextView mTvRemark;
        @BindView(R.id.layout)
        LinearLayout mLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            Utils.setViewLine(itemView);
        }
    }
}
